/**
 * Created by raphael on 15-9-10.
 */
var express = require('express');
var bodyParser = require('body-parser');
var passport = require('passport');
var ldapStrategy = require('passport-ldapauth');
var loginRouter = express.Router();

// create application/json parser
var jsonParser = bodyParser.json();

// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({
    extended: false
});

//ldapauth options here
var OPTS = {
    server: {
        url: 'ldap://ldap.forumsys.com:389',
        bindDn: 'cn=read-only-admin,dc=example,dc=com',
        bindCredentials: 'password',
        searchBase: 'dc=example,dc=com',
        searchFilter: '(uid={{username}})'
    }
}; //still need mod



loginRouter.use(jsonParser);
loginRouter.use(urlencodedParser);
loginRouter.use(passport.initialize());

passport.use(new ldapStrategy(OPTS));
// welcome message
//loginRouter.get('/', function(req, res) {
//    res.json({msg: 'login username pls'});
//});

//console.log('here');


loginRouter.post('/', passport.authenticate('ldapauth', {
    session: false
}), function (req, res) {
    res.send({status: 'ok'});
});


module.exports = loginRouter;